package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.entity.ApplicationRecord;
import com.woniuxy.factory.entity.PurchaseRequest;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.mapper.ApplicationRecordMapper;
import com.woniuxy.factory.mapper.PurchaseRequestMapper;
import com.woniuxy.factory.service.PurchaseRequestService;
import com.woniuxy.factory.util.ApplicationType;
import com.woniuxy.factory.util.AuditType;
import com.woniuxy.factory.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 邹佳骏
 * @date 2020-08-17 12:18
 */
@Service
public class PurchaseRequestServiceImp implements PurchaseRequestService {

    @Autowired
    PurchaseRequestMapper purchaseRequestMapper;

    /*注入申请类型*/
    @Autowired
    ApplicationRecordMapper applicationRecordMapper;


    @Override
    public List<PurchaseRequest> getPurchaseRequestAll(int page, int limit
            ,PurchaseRequest purchaseRequest,String searchDate)throws RuntimeException {

        //获取起始时间和结束时间
        String startTime =null ;
        String endTime  = null;
        //非空验证
        if(searchDate!=null&&searchDate!=""){
            startTime = searchDate.substring(0,10);
            endTime = searchDate.substring(13,23);
            System.out.println("startTime:"+startTime);
            System.out.println("endTime:"+endTime);
        }
        /*查询总条数*/
        int allCount = purchaseRequestMapper.getAllCount();


        /*获取当前的总页数*/
        int totalPage = 0;
        if(allCount % limit ==0){
            totalPage = allCount / limit;
        }else{
            totalPage = (allCount / limit)+1;
        }

        List<PurchaseRequest> purchaseRequests ;
        /*判断当前页数是否大于总页数*/
        if(page<=totalPage) {
            int home = (page - 1) * limit;
            System.out.println("home:"+home);
            purchaseRequests = purchaseRequestMapper.getPurchaseRequestAll(home, limit,purchaseRequest,startTime,endTime);
        }else {
            throw new RuntimeException("页面不存在");
        }

        return purchaseRequests;
    }

    /*查询总条数*/
    @Override
    public int selectAllCount() {
        int allCount = purchaseRequestMapper.getAllCount();
        return allCount;
    }

    /*开启事务，在插入采购申请表的同时在采购类型记录表插入数据*/
    @Transactional
    @Override
    public ResponseResult insertPurchase(String proposer, Date requesttime, String requestremark) {

        ResponseResult responseResult = new ResponseResult();
        PurchaseRequest purchaseRequest = new PurchaseRequest();
        ApplicationRecord applicationRecord = new ApplicationRecord();

        //获取16位编码作为采购申请的编号
        String purchaseNumber = UuidUtil.get16UUID();
        //获取16位编码作为申请类型的主键
        String aprId = UuidUtil.get16UUID();

        //为采购申请表对象赋值
        purchaseRequest.setRequestnumber(purchaseNumber);
        purchaseRequest.setProposer(proposer);
        purchaseRequest.setRequestremark(requestremark);
        purchaseRequest.setRequesttime(requesttime);

        //为申请类型记录表对象赋值
        applicationRecord.setAprId(aprId);
        applicationRecord.setApplicationtype(ApplicationType.PURCHASEREQUEST);
        applicationRecord.setApplicationnum(purchaseNumber);



        //捕获异常
        try {
            //调用方法插入数据
            purchaseRequestMapper.insertPurchase(purchaseRequest);
            applicationRecordMapper.insertApplicationRecord(applicationRecord);
        }catch (RuntimeException e){
            responseResult.setMsg("插入失败，请检查格式");
            return responseResult;
        }
        responseResult.setMsg("插入成功");
        responseResult.setCode(AuditType.No_Approval);
        return responseResult;
    }
}
